<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const cai = {
        uname: '只因',
        age: 2.5,
        hobbit: ['sing', 'dance', 'rap', 'basketball'],
        fans: {
          uname: 'ikun',
          age: 9999,
          hobbit: ['smoke', 'drink', 'makeHairStyle'],
        },
      }
      //深拷贝：就是不断做浅拷贝的操作，直到所有的属性都是简单数据类型
      //递归
      const yyx = {}
      function deepClone(newObject, oldObject) {
        for (let key in oldObject) {
          if (oldObject[key] instanceof Array) {
            newObject[key] = []
            deepClone(newObject[key], oldObject[key])
          } else if (oldObject[key] instanceof Object) {
            newObject[key] = {}
            deepClone(newObject[key], oldObject[key])
          } else {
            newObject[key] = oldObject[key]
          }
        }
      }
      deepClone(yyx, cai)
      // console.log(yyx)
      yyx.fans.hobbit[2] = '九九八十一难'
      console.log(yyx)
      console.log(cai)

      //
      // const yyx = JSON.parse(JSON.stringify(cai))
      // console.log(yyx)
    </script>
  </body>
</html>
